package trade.presenter;

import android.support.annotation.NonNull;

import Utils.ActivityUtil;
import Utils.HelloWordModel;
import Utils.SharedPrefStore;
import dao.BaseOutput;
import dao.CustomIdInput;
import dao.TradeRecordOutput;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import trade.view.TradeRecordView;

/**
 * by BF00233 on 2018/12/21.
 */
public class TradeRecordPresenter {

    private final TradeRecordView iView;

    public TradeRecordPresenter(TradeRecordView view) {
        iView = view;
    }

    public void getTradeRecord() {
        iView.showProgress();
        SharedPrefStore store = new SharedPrefStore();
        CustomIdInput input = new CustomIdInput();
        input.customId = Long.valueOf(store.getString("id", "0"));

        HelloWordModel.getInstance(iView.getHostActivity())
                .getTradeRecord(input).enqueue(new Callback<BaseOutput<TradeRecordOutput>>() {
            @Override
            public void onResponse(@NonNull Call<BaseOutput<TradeRecordOutput>> call, @NonNull Response<BaseOutput<TradeRecordOutput>> response) {
               try{
                if (ActivityUtil.isActivityDestroyed(iView.getHostActivity())) {
                    return;
                }
                iView.hideProgress();
                BaseOutput<TradeRecordOutput> body = response.body();
                if (body == null) {
                    return;
                }
                if ("success".equals(body.msg) && body.data != null) {
                    if (body.data.getItems() != null) {
                        iView.showTradeRecord(body.data.getItems());
                    }
                }
               } catch (Exception e) {
                   e.printStackTrace();
               }
            }

            @Override
            public void onFailure(@NonNull Call<BaseOutput<TradeRecordOutput>> call, @NonNull Throwable t) {
                iView.hideProgress();
            }
        });

    }

}
